
var pageSize = 10,pageNum = 1,canScroll=false,id = parseInt(GetUrlString("id"));

function GetGroup(){
  $.ajax({
      type:'GET',
      url:address+'/teambuyBuildings/list4Map.do?format=true',
      data:{
        "pageSize":pageSize,
        "pageNum":pageNum
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.list,html="";
          $.each(basedata,function(index,info){
              var img = info.microImage ? info.microImage+'&size=300x240' : noImg ;
              html+= '<li><a href="groupPurchase-details.html?id='+info.id+'">'+
              // '<span class="sort">TOP '+info.sort+'</span>'+
              '<img src="'+img+'">'+
              '<p class="name">'+info.title+'</p>'+
              '<p>均价: <span class="red">'+info.averagePrice+'</span></p>'+
              '<p class="address">'+info.address+'</p>'+
              '<p class="countdown"><span>'+info.day+'</span>天<span>'+info.hour+'</span>时<span>'+info.minute+'</span>分</p>'+
              '<div class="sign-up" data-id="'+info.id+'">立即报名</div>'+
              '<p class="signed-up">已有<span class="red">'+info.attentPeople+'</span>人报名</p>'+
            '</li></a>';
          })
          $("#groupList").html(html);
        }
      }
  })
}

// 获取首页轮播图
GetGroup.prototype.getSlide =function(){
  $.ajax({
      type:'get',
      url:address+'/slide/listSlides.do?sence=17&format=true',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          $("#slide").html(listSlides(result.data));
          slide($("#slide"));
        }
      }
  })
};

function listSlides(data){
  var data = data.slideList,html='';
  html +='<ul class="slideBox">';
    $.each(data,function(index,info){
      html +='<li><a href="'+info.clickUrl+'"><div class="cover"></div><img src="'+info.image+'&size=640x300"></a></li>';
    })      
  html +='</ul><div class="point">';
  for(var i=0;i<data.length;i++){
    html += '<span></span>';
  }
  html +='</div>';
  return html;
};

//图片点击——>详情页
$(".groupPurchase-btm-ul li img").on("click",function(){
  window.location.href="groupPurchase-details.html?id=1010006000001002";
})

//我要报名点击
$("#groupList").on("click",".sign-up",function(e){
    e.preventDefault();
    var id = $(this).attr("data-id");
    $(".coverDig").show();
    $("#teambuyId").val(id);
})
//我要报名点击 详情页
$("#time").on("click","#sign-up",function(){
    var id = $(this).attr("data-id");
    $(".coverDig").show();
    $("#teambuyId").val(id);
})

$("#attent").on("click",function(){
    var pass = true;
    var checkinput = $("#attentCdn").find("input[type='text']");
    if(!checkNull(checkinput)) return;

    checkinput.each(function(){
        if(!checkForm($(this))){
          pass = false;
          return false;
        }
    });
    if(!pass) return false;

    var data = $("#attentCdn").serialize();
    $.ajax({
      type:'GET',
      url:address+'/teambuyBuildings/attent.do',
      data:data,
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          groupDetail(id);
          $(".coverDig").hide();
        } 
        message(result.message);
      }
    })
})

//称呼点击
$(".signIn-call label").on("click",function(){
  $(".signIn-call label span").removeClass("on");
  $(this).children("span").addClass("on");
})


// 团购详情
function groupDetail(id){
  var token = localStorage.getItem("hpWrite_token");
  $.ajax({
      type:'GET',
      url:address+'/teambuyBuildings/getDetails.do?format=true',
      data:{
        "id":id
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.teambuyBuildings;
          var d = new Detail(basedata);
          d.detailMap();
          $("#slide").html(d.slideTPL());
          $("#time").html(template("timeTPL",basedata));
          $("#favourableDetails").html(basedata.favourableDetails);
          $("#seeGroup").html(template("seeGroupTPL",basedata));
          $("#buildingsDetails").html(basedata.buildingsDetails);
          $("#newBuildings").html(template("newBuildingsTPL",basedata));
        }
      }
  })
};

function Detail(data){
  this.data = data;
}
// 轮播图
Detail.prototype.slideTPL = function(){
    var html ='',data = this.data;
    html += '<ul class="slideBox">';
    $.each(data.teambuyImageList,function(index,info){
      html += '<li><img src="'+info+'&size=640x300"></li>';
    })
      html += '</ul>'+
      '<div class="tips p-lr10 white l-h24 abs b0 l0 w100p bg-white">'+
        '<span class="name">'+data.name+'</span>'+
        '<span class="item white abs r10 t0"><span class="current">1</span><span>&frasl;</span><span class="all">1</span></span>'+
      '</div>';
    return html;
}
// 地图
Detail.prototype.detailMap = function(){
  var data = this.data;
  var map = new BMap.Map("detailMap");
  var point = new BMap.Point(data.longitude,data.latitude);
  map.centerAndZoom(point,16);
  var marker = new BMap.Marker(point);  // 创建标注
  map.addOverlay(marker);              // 将标注添加到地图中
  var label = new BMap.Label(data.name,{offset:new BMap.Size(20,-10)});
  marker.setLabel(label);
};

//户型图
Detail.prototype.houseImg = function(){
    var basedata = this.data;
    $("#houseImgList").html(houseImgTPL(basedata));
    
    var $li = $("#houseImgList").find("li"),
        w = $("#houseImg").width(),
        l = $li.length;
    $li.width(w/2-15);
    $("#houseImgList").width(w*l/2);
    //户型图滑动
    var myScroll = new IScroll('#houseImg', {
        scrollbars: false,
        mouseWheel: false,
        momentum:false,
        scrollX:true,
        scrollY:false,
        disableMouse:true,
        shrinkScrollbars: 'clip',
    }); 
};

function houseImgTPL(data){
  var html ='';
  $.each(data,function(index,info){
    html += '<li><img class="pic" src="'+info.image+'&size=280x280"></li>';
  })
  html +='</li>';
  return html;
};

//轮播图do
function doDetailSlide(){
  var slength = $('#slide').find('li').length,
      $item = $('#slide').find('.item'),
      current = 1;
  $item.find('.all').text(slength);
  var slider = Swipe(document.getElementById('slide'), {
    auto: 3000,
    callback:function(index) {
      current ++;
      current = current > slength ? 1 : current;
      $item.find('.current').text(current);
    }
  });
};

// 优惠详情更多
$("#favorableMore").click(function(){
    var $this = $(this);
    if($this.hasClass("turnDown")){
      $this.removeClass("turnDown");
      $("#favourableDetails").addClass("favourableH");
    }else{
      $this.addClass("turnDown");
      $("#favourableDetails").removeClass("favourableH");
    }
})

